// ajax get 方式的封装
function ajaxGet(url, cb, data) {
    let str = '';
    data = data || {};
    for (let i in data) {
        str += `${i}=${data[i]}&`;
    }
    url += "?" + str + "collette=" + Date.now();
    let xhr = new XMLHttpRequest();
    xhr.open('get', url);
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
            cb(xhr.responseText);
        }
    }
    xhr.send();
}


// Ajax post方式的封装
function ajaxPost(url, cb, data) {
    data = data || {};
    let str = '';
    for (let i in data) {
        str += `${i}=${data[i]}&`;
    }
    let xhr = new XMLHttpRequest();
    xhr.open('post', url);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
            cb(xhr.responseText);
        } else if (xhr.readyState === 4 && xhr.status === 200) {
            cb("error:" + xhr.status);
        }
    }
    xhr.send(str);
}

//get和post二合一
function ajaxRequest(url, type, cb, data) {
    data = data || {};
    let str = '';
    for (let i in data) {
        str += `${i}=${data[i]}&`;
    }
    let xhr = new XMLHttpRequest();
    if (type === 'get') {
        url += '?' + str + 'collette:' + Date.now();
    }
    xhr.open(type, url);
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
            cb(xhr.response);
        } else if (xhr.readyState === 4 && xhr.status !== 200) {
            cb('error:' + xhr.status);
        }
    }
    if (type === 'post') {
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.send(str);
    } else {
        xhr.send();
    }

}

/********************ajax的get,post,jsonp三合一封装 ****************/
// 设计执行方式
// 请求类型
// 请求地址
// 发送数据
// 请求成功
// 请求失败
// 超时


// 默认参数：利用ES6的解构赋值时，指定默认值
function ajax({
    type = "get",
    url,
    data = {},
    success,
    error,
    timeout = 50
}) {
    // 根据参数的判断，决定执行get或post或jsonp的功能
    // console.log(type,url,data,success,error,timeout);

    let str = "";
    for (let i in data) {
        str += `${i}=${data[i]}&`;
    }

    if (type !== "post") {
        url = url + "?" + str + "_qft_=" + Date.now();
    }

    if (type === "jsonp") {
        // jsonp
        let script = document.createElement("script");
        script.src = url;
        document.body.appendChild(script);

        window[data[data.col]] = function (res) {
            success && success(res);
            error = null;
        }
    } else {
        // ajax
        let ajax = new XMLHttpRequest();
        ajax.open(type, url, true);
        ajax.onreadystatechange = function () {
            if (ajax.readyState === 4 && ajax.status === 200) {
                success && success(ajax.responseText);
                error = null;
            } else if (ajax.readyState === 4 && ajax.status !== 200) {
                error && error(ajax.status);
                success = null;
            }
        }
        if (type === "get") {
            ajax.send();
        } else {
            ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
            ajax.send(str.slice(0, -1));
        }
    }
    setTimeout(() => {
        error && error("timeout");
        success = null;
    }, timeout);
}


//jsonp解决跨域问题的封装
function jsonp(url, data, cb) {
    let str = '';
    for (let i in data) {
        str += `${i}=${data[i]}&`;
    }
    url += '?' + str + 'collette=' + Date.now();

    let script = document.createElement('script');
    script.src = url;
    document.body.appendChild(script);
    window[data[data.columnName]] = function (res) {
        cb(res);
        script.remove();
        window[data[data.columnName]] = '';
    }
}